﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using ClassLibrary1_DAL;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            //add 3 cuon sach
            DB.SetQuery("INSERT INTO Book VALUES(@name,@price)");
            DB.AddParameters("name", "abc");
            DB.AddParameters("price", "123456");
            DB.ExeNoneQuery();

            DB.SetQuery("INSERT INTO Book VALUES(@name,@price)");
            DB.AddParameters("name", "bcd");
            DB.AddParameters("price", "123456");
            DB.ExeNoneQuery();

            DB.SetQuery("INSERT INTO Book VALUES(@name,@price)");
            DB.AddParameters("name", "def");
            DB.AddParameters("price", "123456");
            DB.ExeNoneQuery();

            List<Book> book = new List<Book>();
            DB.SetQuery("SELECT * From Book");
            List<List<object>> rs = new List<List<object>>();
            rs = DB.ExeQuery();
            foreach (List<object> lo in rs)
            {
                Book b = new Book();
                b.Id = Convert.ToInt16(lo[0]);
                b.Name = Convert.ToString(lo[1]);
                b.Price = Convert.ToDouble(lo[2]);
                book.Add(b);
            }
            dataGridViewX1.DataSource = book;



            

            
            sbtStatus.Value = true;

        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            ///update 1 cuon
            DB.SetQuery("UPDATE Book SET Name = @name, Price = @price WHERE id = @id");
            DB.AddParameters("name", "NameM");
            DB.AddParameters("id", "2");
            DB.ExeNoneQuery();

            List<Book> book = new List<Book>();
            DB.SetQuery("SELECT * From Book");
            List<List<object>> rs = new List<List<object>>();
            rs = DB.ExeQuery();
            foreach (List<object> lo in rs)
            {
                Book b = new Book();
                b.Id = Convert.ToInt16(lo[0]);
                b.Name = Convert.ToString(lo[1]);
                b.Price = Convert.ToDouble(lo[2]);
                book.Add(b);
            }
            dataGridViewX2.DataSource = book;

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            ///delete 1 cuon
            ///
            DB.SetQuery("DELETE FROM Book WHERE ID = @id");
            DB.AddParameters("id", "3");
            DB.ExeNoneQuery();

            List<Book> book = new List<Book>();
            DB.SetQuery("SELECT * From Book");
            List<List<object>> rs = new List<List<object>>();
            rs = DB.ExeQuery();
            foreach (List<object> lo in rs)
            {
                Book b = new Book();
                b.Id = Convert.ToInt16(lo[0]);
                b.Name = Convert.ToString(lo[1]);
                b.Price = Convert.ToDouble(lo[2]);
                book.Add(b);
            }
            dataGridViewX3.DataSource = book;
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            string connectS = @"Server="+txtIP.Text+"; Database = testSkill_data; User ID = truongnln; Password = 123456";
            try
            {
                SqlConnection con = new SqlConnection(connectS);
                con.Open();
                con.Close();
                sbtStatus.Value = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                sbtStatus.Value = false;
            }
        }
        
    }

    public class Book
    {
        public int Id { set; get; }
        public string Name { set; get; }
        public double Price { set; get; }

        public Book(int id = 0, string name = null, double price = 0)
        {
            Id = id;
            Name = name;
            Price = price;
        }
    }
}
